import { setAreaStyle } from "@/ol-utils/index.js";
import { useCityStore } from "@/stores/city.js";
import { getWeatherMapHttp } from "@/request/index.js";
import { ref } from "vue";
import { getLocationByCityHttp } from "@/request/index.js";
export const useGeoLoactionHook = async ({ map }) => {
  const weather = ref("");

  const $store = useCityStore();
  let city = $store.city + "市";
  /* 获取逆向数据 */
  let res = await getLocationByCityHttp(city);
//   console.log(res);
  var { adcode, location } = res.geocodes[0];
  // let center = location.split(","); //将字符串切割成数组
  let res1 = location.split(",").map(item=>+item)//[109.120248, 21.481305]
  // ol.proj.fromLonLat() 是 OpenLayers 中的一个函数，它的主要作用是将地理坐标（经纬度）转换为投影坐标。
  let center = ol.proj.fromLonLat(res1,"EPSG:4326") 
  //第二个参数为投影，默认情况下，OpenLayers 使用 'EPSG:3857'（Web 墨卡托投影）。本项目使用的是"EPSG:4326"
  console.log(res1)
  console.log(center,'proj')
  // console.log(location,'location' )
 
  /* 需要传入map,adcode,center */
  /* 1、geojson数据加载 */
  /* 2、执行飞行视角 */
  // console.log(map)
  /* 飞行视角 */
  map.getView().animate({
    center,
    zoom: 8,
    duration: 2000,
  });
  let url = `https://geo.datav.aliyun.com/areas_v3/bound/${adcode}_full.json`;
  /* 加载geojson数据 */
  const layer = new ol.layer.Vector({
    source: new ol.source.Vector({
      format: new ol.format.GeoJSON(),
      url,
    }),
    style: setAreaStyle({}),
  });
  map.addLayer(layer);

  /* 获取天气数据 */
  getWeatherMapHttp($store.city).then((res)=>{
    console.log($store.city)
    console.log(res.lives[0])
    weather.value = res.lives[0]
  })
  return { weather };
};
